package com.titaniume.java2;

import org.junit.Test;

import java.time.*;
import java.time.format.DateTimeFormatter;
import java.time.temporal.TemporalAdjuster;
import java.time.temporal.TemporalAdjusters;
import java.util.Set;

/**
 *@program: java8
 *@description:
 *@author: renfei.huang
 *@create: 2021-11-01 14:01
 */
public class TestLocalDateTime {


    //1.LocalDate  LocalTime  LocalDateTime
    @Test
    public void test1() {

        LocalDateTime ldt = LocalDateTime.now();
        System.out.println(ldt);

        LocalDateTime ldt2 = LocalDateTime.of(2021, 11, 11, 13, 22, 45);
        System.out.println(ldt2);

        LocalDateTime ldt3 = ldt.plusYears(2);
        System.out.println(ldt3);

        LocalDateTime ldt4 = ldt.minusMonths(4);
        System.out.println(ldt4);

        System.out.print(ldt.getYear() + "\t");
        System.out.print(ldt.getMonth() + "\t");
        System.out.print(ldt.getDayOfMonth() + "\t");
        System.out.print(ldt.getHour() + "\t");
        System.out.print(ldt.getMinute() + "\t");
        System.out.print(ldt.getSecond() + "\t");
    }

    //2.Instant:时间戳 已unix 元年 1970年1月1日 00:00:00 到某个时间直接的毫秒差
    @Test
    public void test2() {
        Instant ins1 = Instant.now(); //默认获取的是UTC时区 差8小时
        System.out.println(ins1);

        OffsetDateTime offsetDateTime = ins1.atOffset(ZoneOffset.ofHours(8));
        System.out.println(offsetDateTime);

        System.out.println(ins1.toEpochMilli());

        Instant ins2 = Instant.ofEpochSecond(1000);
        System.out.println(ins2);

    }

    //3.
    //Duration: 计算两个时间直接的间隔
    //Period: 计算两个日期直接的间隔
    @Test
    public void test4() {

        LocalDate ld1 = LocalDate.of(2017, 01, 01);
        LocalDate ld2 = LocalDate.now();

        Period p = Period.between(ld1, ld2);
        System.out.println(p);
        System.out.println(p.getYears());
        System.out.println(p.getMonths());
        System.out.println(p.getDays());
    }

    @Test
    public void test3() {

        Instant ins1 = Instant.now();
        try {
            Thread.sleep(1000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        Instant ins2 = Instant.now();

        Duration duration = Duration.between(ins1, ins2);
        System.out.println(duration);

        System.out.println(duration.toHours());

        System.out.println("--------------------");


        LocalDateTime ldt1 = LocalDateTime.now();

        try {
            Thread.sleep(1000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        LocalDateTime ldt2 = LocalDateTime.now();
        System.out.println(Duration.between(ldt1, ldt2).toMillis());
    }

    //4. TemporalAdjuster : 时间校正器
    @Test
    public void test5() {

        LocalDateTime ldt1 = LocalDateTime.now();
        System.out.println(ldt1);
        LocalDateTime ldt2 = ldt1.withDayOfMonth(10);
        System.out.println(ldt2);

        //下一个周日
        LocalDateTime ldt3 = ldt1.with(TemporalAdjusters.next(DayOfWeek.SUNDAY));
        System.out.println(ldt3);

        //自定义: 下一个工作日是什么时候
        LocalDateTime ldt5 = ldt3.with((l) -> {
            LocalDateTime ldt4 = (LocalDateTime) l;
            //获取周几
            DayOfWeek dayOfWeek = ldt4.getDayOfWeek();
            if (dayOfWeek.equals(DayOfWeek.FRIDAY)) {
                return ldt4.plusDays(3);
            } else if (dayOfWeek.equals(DayOfWeek.SATURDAY)) {
                return ldt4.plusDays(2);

            } else {
                return ldt4.plusDays(1);
            }
        });

        System.out.println("下一个工作日：" + ldt5);
    }


    //5. DateTimeFormatter : 解析和格式化日期或时间

    @Test
    public void test6() {

//        DateTimeFormatter dtf = DateTimeFormatter.ISO_LOCAL_DATE;
        DateTimeFormatter dtf = DateTimeFormatter.ISO_DATE;

        LocalDateTime ldt = LocalDateTime.now();
        String strDate = ldt.format(dtf);
        System.out.println(strDate);


        System.out.println("-----------");


        DateTimeFormatter stf2 = DateTimeFormatter.ofPattern("yyyy年MM月dd日 HH:mm:ss");

        String strData2 = stf2.format(ldt);
        System.out.println(strData2);


        LocalDateTime newDate = ldt.parse(strData2, dtf);
        System.out.println(newDate);

    }


    //6.ZonedDate、ZonedTime、ZonedDateTime ： 带时区的时间或日期
    @Test
    public void test7(){
        Set<String> set = ZoneId.getAvailableZoneIds();
        set.forEach(System.out::println);
    }


    @Test
    public void test8() {
        LocalDateTime ldt = LocalDateTime.now(ZoneId.of("Asia/Shanghai"));
        System.out.println(ldt);

        LocalDateTime ldt2 = LocalDateTime.now(ZoneId.of("Asia/Shanghai"));
        ZonedDateTime zdt = ldt2.atZone(ZoneId.of("Asia/Shanghai"));
        System.out.println(zdt);

    }


}
